package com.example;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Stat;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class ZookeeperTest {


    private ZooKeeper zooKeeper;

    @BeforeEach
    public void before() throws IOException {
        zooKeeper = new ZooKeeper("192.168.228.101:2181", 5000, new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                if (Event.EventType.None == event.getType()) {
                    if (event.getState() == Event.KeeperState.SyncConnected) {
                        System.out.println("创建连接成功");
                    } else if (event.getState() == Event.KeeperState.Disconnected) {
                        System.out.println("断开连接");
                    } else if (event.getState() == Event.KeeperState.Expired) {
                        System.out.println("会话超时");
                    } else if (event.getState() == Event.KeeperState.AuthFailed) {
                        System.out.println("认证失败");
                    }
                }
                System.out.println(event.getPath());
                System.out.println(event.getType());
            }
        });
        System.out.println(zooKeeper.getSessionId());

    }

    @Test
    public void test() throws KeeperException, InterruptedException {
        List<String> children = zooKeeper.getChildren("/", true);
        System.out.println(children);

        TimeUnit.SECONDS.sleep(10);
    }

    @Test
    public void test2() throws KeeperException, InterruptedException {
        List<String> children = zooKeeper.getChildren("/", new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("自定义回调");
            }
        });
        System.out.println(children);

        TimeUnit.MINUTES.sleep(10);
    }

    @Test
    public void test3() throws KeeperException, InterruptedException {
        String path = zooKeeper.create("/idea", "abba".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);//EPHEMERAL临时节点
        System.out.println(path);
        TimeUnit.MINUTES.sleep(10);
    }

    @Test
    public void test4() throws KeeperException, InterruptedException {
        byte[] data = zooKeeper.getData("/zookeeper", true, new Stat());
        System.out.println(new String(data));
        TimeUnit.MINUTES.sleep(10);
    }

    @Test
    public void test5() throws KeeperException, InterruptedException {
        Stat stat = zooKeeper.setData("/zookeeper", "true".getBytes(), 0);
        System.out.println(stat);
    }

    @Test
    public void test6() throws KeeperException, InterruptedException {
        Stat stat = zooKeeper.exists("/zookeeper", false);
        System.out.println(stat);
    }

    @Test
    public void test7() throws KeeperException, InterruptedException {
        zooKeeper.delete("/a", 4);
    }



    /**
     * 循环注册
     */
    public void register() throws KeeperException, InterruptedException {
        byte[] data = zooKeeper.getData("/a", new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                try {
                    register();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, null);
        System.out.println(new String(data));




    }

    @Test
    public void test8() throws KeeperException, InterruptedException {
        register();
        TimeUnit.MINUTES.sleep(10);

    }

    @Test
    public void test9() throws Exception {
        List<ACL> acl = zooKeeper.getACL("/a", new Stat());
        System.out.println(acl);
    }


}
